const express = require('express');
const router = express.Router();
const bcrypt = require('bcrypt-nodejs');
const UserModel = require('../db/UserModel');//用户模型
const authhs256 = require("../middlewares/authhs256/authentication")
const moment = require('moment');
const OperationLog = require("../db/OperationLog");
const now = () => {
    return moment().format('YYYY-MM-DD HH:mm:ss')
}
const CmmuseumModel = require('../db/CmmuseumModel');
const RoleModel = require('../db/RoleModel');
//登录
router.post('/', (req, res, next) => {
    let { phone, password, storeId } = req.body
    if (!phone || !password || !storeId) {
        return res.json({ code: 201, mes: '缺少必要信息' });
    }
    let pare;
    let arrs = [];
    let ids = [];
    UserModel.findOne({ phone }).then(function (user) {
        if (!user) {
            return res.json({ code: 202, mes: '帐号或密码输入错误.' });
        };
        return RoleModel.findById(user.role).then(role => {
            return CmmuseumModel.findById(storeId).then(cmmus => {
                pare = {
                    id: user._id,
                    name: user.name,
                    roles: user.role,
                    ip: req.ip.replace(/::ffff:/, ''),
                    time: now(),
                    Operationcontent: "登陆成功."
                };
                if (cmmus.state == "禁用") {
                    if (role.name == "医生") {
                        return res.json({ code: 202, mes: '该门店已被禁用.' });
                    }
                }
                if (user.state == "禁用") {
                    if (role.name == "医生") {
                        return res.json({ code: 202, mes: '该账号已被禁用.' });
                    }
                };
                if (role.state == "禁用") {
                    if (role.name != "超级管理员") {
                        return res.json({ code: 202, mes: '该账用户角色被禁用.' });
                    }
                };
                arrs = user.shop;
                if (!arrs.includes(storeId)) {
                    return res.json({ code: 202, mes: '账户与门店不匹配.' });
                }
                if (!bcrypt.compareSync(password, user.password)) {
                    pare.Operationcontent = "帐号或密码输入错误.";
                    OperationLog.create(pare).then(data => { }).catch(e => { })
                    return res.json({ code: 203, mes: '帐号或密码输入错误.' });
                }
                OperationLog.create(pare).then(data => { }).catch(e => { })
                ids.push(storeId)
                return res.json({
                    code: 200,
                    mes: '登录成功.',
                    token: authhs256.createJWT(user),
                    Userinfo: {
                        name: user.name,
                        id: user._id,
                        shop: ids,
                        jurisdiction: user.jurisdiction,
                        role: user.role
                    }
                });
            })

        })
    }).catch(function (err) {
        return res.json({ code: 204, mes: '登陆失败!', err });
    });
});

module.exports = router;